package com.gxuwz.weatherapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class HourlyForecastAdapter extends RecyclerView.Adapter<HourlyForecastAdapter.HourlyViewHolder> {
    
    public static class HourlyForecast {
        String time;
        int weatherIconRes;
        String temperature;

        public HourlyForecast(String time, int weatherIconRes, String temperature) {
            this.time = time;
            this.weatherIconRes = weatherIconRes;
            this.temperature = temperature;
        }
    }

    private final List<HourlyForecast> forecasts = new ArrayList<>();

    public void updateData(List<HourlyForecast> newForecasts) {
        forecasts.clear();
        forecasts.addAll(newForecasts);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HourlyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.hourly_forecast_item, parent, false);
        return new HourlyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HourlyViewHolder holder, int position) {
        HourlyForecast forecast = forecasts.get(position);
        holder.bind(forecast);
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }

    static class HourlyViewHolder extends RecyclerView.ViewHolder {
        private final TextView timeText;
        private final ImageView weatherIcon;
        private final TextView tempText;

        public HourlyViewHolder(@NonNull View itemView) {
            super(itemView);
            timeText = itemView.findViewById(R.id.timeText);
            weatherIcon = itemView.findViewById(R.id.weatherIcon);
            tempText = itemView.findViewById(R.id.tempText);
        }

        public void bind(HourlyForecast forecast) {
            timeText.setText(forecast.time);
            weatherIcon.setImageResource(forecast.weatherIconRes);
            tempText.setText(forecast.temperature);
        }
    }
} 